<?php
namespace Napi\Controller;
use Think\Controller;
class IndexController extends NapiController {
    /**
     * 所有频道
     * @author houqiang
     */
    public function channels(){
        $list = D('category')->where('pid=40 and status=1')->select();
        foreach ($list as $k =>$v){
            if ($v['id'] == 1){
                $tuijian[$k]['cid'] = $v['id'];
                $tuijian[$k]['cname'] = $v['title'];
                $tuijian[$k]['logo'] = '1';
            }else{
                $data[$k]['cid'] = $v['id'];
                $data[$k]['cname'] = $v['title'];
                $data[$k]['logo'] = '0';
                if ($data[$k]['cid'] == ''){
                    unset($data[$k]);
                }
            }

        }
        $res['list'] =array_values($tuijian);
        $res['tuijian'] = array_values($data);

        $this->ReturnClient(2,'频道！',$res);
    }

    /**
     * 频道列表
     * @author houqiang
     */
    public function lists(){
        $cid = $_REQUEST['cid'];
        $pageNo =$_REQUEST['pageNo']==''?'1':$_REQUEST['pageNo'];
        $pageSize =$_REQUEST['pageSize']==''?'10':$_REQUEST['pageSize'];
        $list = D('document')->where('category_id='.$cid)->order('id desc')->limit($pageNo,$pageSize)->select();
        foreach ($list as $k=>$v){
            $data[$k]['id'] = $v['id'];
            $data[$k]['title'] = $v['title'];
            $data[$k]['logo'] = $v['cover'];
            $data[$k]['summary'] = $v['description'];
            $data[$k]['created_at'] = $v['create_time'];
            $data[$k]['source'] = $v['source'];
            $data[$k]['view'] = $v['view'];
        }
        $this->ReturnClient(2,'频道列表！',$data);
    }

    /*
     * 我的文章
     * @author houqiang
     */
    public function my_article(){
        $uid = $_REQUEST['uid'];
        $result = D('document')->where('uid='.$uid)->select();
        if ($result){
            $this->ReturnClient(2,'我的文章！',$result);
        }else{
            $this->ReturnClient(1,'无文章！');
        }
    }


    /*
     * 用户详情动态
     *
     */
    public function dynamic(){

    }

    /*
         * 关注
         *  关注类型  用户关注 to_user 话题关注 to_topic
         * @author houqiang
         */
    public function follow(){
        $data['user_id']  = $_REQUEST['uid'];
        $data['to_id']  = $_REQUEST['to_id'];
        $data['type']  = $_REQUEST['type'];
        $sql = D('follow')->where($data)->find();
        if ($sql){
            D('follow')->where('id='.$sql['id'])->delete();
            $this->ReturnClient(2,'取消关注成功！');
        }
        $data['created_at'] = time();
        $result = D('follow')->add($data);
        if ($result){
            if ($data['type'] == 'to_user'){
                D('ucenter_member')->where('id='.$data['user_id'])->setInc('follow',1);
                D('ucenter_member')->where('id='.$data['to_id'])->setInc('cover_follow',1);
                self::messages($data['user_id'],$data['type'],$result,$data['to_id'],'用户关注成功!','关注了你','follow','ucenter_member');
            }
            if ($data['type'] == 'to_topic'){
                D('community')->where('id='.$data['to_id'])->setInc('follow',1);
                 self::messages($data['user_id'],$data['type'],$result,$data['to_id'],'话题关注成功!','关注了您的话题','follow','community');
            }

            $this->ReturnClient(2,'关注成功！',$result);
        }else{
            $this->ReturnClient(1,'关注失败！');
        }
    }

    /*
     * 我的关注
     * @author houqiang
     */

    public function messages($uid,$type='',$to_id,$data_id,$msg,$content,$action,$model){
        $data['user_id'] = $uid;
        $data['type'] = $type;
        $data['to_id'] = $data_id;
        $data['data_id'] = $to_id;
        $data['msg'] = $msg;
        $data['content'] = $content;
        $data['action'] = $action;
        $data['model'] = $model;
        $data['created_at'] = time();
        //var_dump($data);die;
        D('messages')->add($data);

    }

    public function my_follow(){
        $uid = $_REQUEST['uid'];
        $sql = D('follow')->where('user_id='.$uid)->select();
        if (!$sql){
            $this->ReturnClient(1,'无关注！');
        }

        foreach ($sql as $k=>$v) {
            $res = D('ucenter_member')->where('id='.$v['to_id'].' and status=1')->find();

            if($res == ''){
                unset($k);
            }else{
                $result[$k]['follow_id'] = $res['id'];
                $result[$k]['username'] = $res['username'];
                $result[$k]['desc'] = $res['desc'];
                $result[$k]['avatar'] = $res['avatar'];
                if (empty($res['desc'])){
                    $result[$k]['desc'] = '这个作者很懒,什么都没写';
                }
            }


        }
        $result =array_values($result);
        $this->ReturnClient(2,'关注列表！',$result);
    }

    /**
     * 注册登录接口
     * @author houqiang
     */
    public function jianjie(){
        $action = $_REQUEST['action'];
        switch ($action) {
            case 'register':
                $username = $_REQUEST['email'];
                $password2 = $_REQUEST['password'];
                $password = md5($password2);
                if ($username == '' || $password == '' || $password2 == '') {
                    $this->ReturnClient(1,'信息为空！');
                }

                $map['username'] = $username;

                $data['username'] = $username;
                $data['password'] = $password;
                $data['email'] = $username;
                $data['create_time'] = time();

                $res = M('ucenter_member')->where($map)->select();

                if ($res) {
                    $this->ReturnClient(1,'用户已存在！');
                }else{
                    $sql1 = D('ucenter_member')->add($data);
                    $sql2 = D('ucenter_member')->where('id='.$sql1)->find();
                    $list['user_id'] = $sql2['id'];
                    $list['username'] = $sql2['username'];
                    $data['avatar'] = '';
                    $data['score'] = $sql2['score'];

                    $this->ReturnClient(2,'注册成功！',$list);
                    //exit(json_encode(''$list)); //返回0表示注册成功
                }
                break;

            case 'login':
                $username = $_REQUEST['email'];

                $password2 = $_REQUEST['password'];

                $password = md5($password2);

                $map['username'] = $username;

                $map['password'] = $password;

                $sql = D('ucenter_member')->where($map)->find();
                $data['user_id'] = $sql['id'];
                $data['username'] = $sql['username'];
                $data['avatar'] = $sql['avatar'];
                $data['score'] = $sql['score'];

                if ($sql) {
                    $this->ReturnClient(2,'登录成功！',$data);
                }else{
                    $this->ReturnClient(1,'登录失败！');
                }
                break;
            default:
                exit(json_encode(error));
                break;
        }
    }

    /*
                        *  md5签名，$array中务必包含 appSecret
                        */

    /**
     * 直播视频列表
     * @author houqiang
     */
    public function video(){
        $pageNo =$_REQUEST['pageNo']==''?'1':$_REQUEST['pageNo'];
        $pageSize =$_REQUEST['pageSize']==''?'10':$_REQUEST['pageSize'];
        $result = D('document')->where('category_id=21')->field('title,cover,view,source,source_url')->limit($pageNo,$pageSize)->select();
        if ($result){
            $this->ReturnClient(2,'视频列表！',$result);
        }else{
            $this->ReturnClient(1,'获取失败！');
        }

    }


    /*
                        *构建参数请求的URL
                        */

    function duiba(){
        $appKey = "z2GgT1PUEZC2pNrUuVVH94ig7Bm";
        $appSecret = "38XAv644wYC7zRkqnkqkXhTddM5t";
        $uid = '1';
        $credits = '0';
        $redirect ='';
        $url = "http://www.duiba.com.cn/autoLogin/autologin?";
        $timestamp=time()*1000 . "";
        //var_dump($timestamp);
        $array=array("uid"=>$uid,"credits"=>$credits,"appSecret"=>$appSecret,"appKey"=>$appKey,"timestamp"=>$timestamp);
        if($redirect!=null){
            $array['redirect']=$redirect;
        }
        $sign=$this->sign($array);
        $array['sign']=$sign;
        $url=$this->AssembleUrl($url,$array);
        $this->ReturnClient(2,'兑吧跳转地址！',$url);
    }


    /*
                        *  生成直达商城内部页面的免登录地址
                        *  通过此方法生成的免登陆地址，可以通过redirect参数，跳转到积分商城任意页面
                        */

    function sign($array){
        ksort($array);
        $string="";
        while (list($key, $val) = each($array)){
            $string = $string . $val ;
        }
        return md5($string);
    }

    /*
     * 用户信息
     */

    function AssembleUrl($url, $array)
    {
        unset($array['appSecret']);
        foreach ($array as $key=>$value) {
            $url=$url.$key."=".urlencode($value)."&";
        }
        return $url;
    }

    /*
     * 头像上传
     */

    public function users(){
        $uid = intval($_REQUEST['uid']);
        $user = D('ucenter_member')->where('id='.$uid)->find();
        if ($user['lately_sign'] ==date("Y-m-d",time())){
            $data['sign'] = '1';
        }else{
            $data['sign'] = '0';
        }
        $data['uid'] = $user['id'];
        $data['username'] = $user['username'];
        $data['sign_days'] = $user['sign_days'];
        $data['score'] = $user['score'];
        if (strpos($user['avatar'],'http') !== false ){
            $data['avatar'] = $user['avatar'];
        }else{
            $data['avatar'] = 'http://'.$_SERVER['HTTP_HOST'].$user['avatar'];
        }

        $data['follow'] = $user['follow'];
        $data['cover_follow'] = $user['cover_follow'];

        $this->ReturnClient(2,'用户信息！',$data);

    }

    function upload_image() {
        $uid = $_REQUEST['uid']==''?'0':$_REQUEST['uid'];
        if ($uid){
            $user = D('ucenter_member')->where('id='.$uid)->find();
        }
        $base64_image = $_REQUEST['file'];
        //$base64 = $_REQUEST['file'];
        //post的数据里面，加号会被替换为空格，需要重新替换回来，如果不是post的数据，则注释掉这一行
        $data = preg_replace('#^data:image/\w+;base64,#i', '', $base64_image);
        //上传配置
        $config = array(
            "savePath" => './Uploads/Picture/',
            "maxSize" => 1000, //单位KB
            "allowFiles" => array(".gif", ".png", ".jpg", ".jpeg", ".bmp")
        );
        $up = new \Uploader($data, $config,true);
        $info = $up->getFileInfo();

        //服务器文件存储路径
        if ($info['state'] == 'SUCCESS'){
            unlink($user['avatar']);
            $user['avatar'] = ltrim($info['url'],'.');
            $mUser =D('ucenter_member');
            $mUser->save($user);
            $image_file = $user['avatar'];
            $this->ReturnClient(2,'上传成功！',$image_file);
        }else{
            $this->ReturnClient(1,'上传失败！');
        }
    }



    /*
     * 获取s评论
     * @author houqiang
     */

    /**
     * 评论
     * @author houqiang
     */
    public function comments(){

        $data['user_id']  = $_REQUEST['uid'];
        $data['type']  = $_REQUEST['type'];
        $data['to_user_id']  = $_REQUEST['to_user_id'];
        $data['content'] = $_REQUEST['content'];
        $data['created_at'] = time();
        $result = D('comments')->add($data);
        if($data['type'] == 'to_topic'){
            //die;
            D('community')->where('id='.$data['to_user_id'])->setInc('pinglun',1);
            self::messages($data['user_id'],$data['type'],$result,$data['to_user_id'],'话题评论成功!',$data['content'],'comments','community');
        }

        if ($data['type'] == 'to_article'){
            $res = D('document')->where('id=' . $data['to_user_id'])->find();
//            var_dump($res['uid']);
            self::messages($data['user_id'],$data['type'],$data['to_user_id'],$res['uid'],'文章评论成功!',$data['content'],'comments','document');
//            self::messages($data['user_id'],$data['type'],$result,$data['to_user_id'],'文章评论成功!',$data['content'],'comments','document');
        }
        if ($result){
            $this->ReturnClient(2,'评论成功！',$result);
        }else{
            $this->ReturnClient(1,'评论失败！');
        }
    }

    /*
    * V友圈 评论信息
    * @author houqiang
    */

    public function comments_list(){
        $data['to_user_id'] = $_REQUEST['to_id'];
        $data['type'] = $_REQUEST['type'];
        $data['status'] = 1;
        $result = D('comments')->where($data)->field('id,user_id,favorites,content,created_at')->select();
        if ($result){
            $this->ReturnClient(2,'文章评论！',$result);
        }else{
            $this->ReturnClient(1,'无评论！');
        }
    }

    /*
     * v达人
     */

    public function vcomments_list(){
        $uid = $_REQUEST['uid']==''?'0':$_REQUEST['uid'];
        $follow = D('follow')->where('user_id='.$uid)->field('to_id')->select();
        var_dump($follow);die;
        $result = D('comments')->where('user_id='.$uid.' and status=1')->field('id,type,user_id,to_user_id,content,created_at')->select();
        if(!$result){
            $this->ReturnClient(1,'无评论！');

        }

        foreach ($result as $k =>$v){
            if ($v['type'] == 'to_article'){
                $res = D('document')->where('id='.$v['to_user_id'])->find();
                $result[$k]['title'] = $res['title'];
                $result[$k]['cover'] = $res['cover'];
                $where = array('to_id'=>$v['to_user_id'],'type'=>'to_article');
                $sql = D    ('praises')->where($where)->select();
                foreach ($sql as $key=>$value){
                    $user[$key] = D('ucenter_member')->where('id='.$value['user_id'])->field('username,avatar')->find();
                    $result[$k]['praises_count'] = count($key);
                    $result[$k]['praises'][] = $user[$key];
                }

            }
            if ($v['type'] == 'to_topic'){
                $res = D('community')->where('id='.$v['to_user_id'])->find();
                $result[$k]['title'] = $res['title'];
                $logo = unserialize($res['logos']);
                $result[$k]['cover'] = $logo[0];
                $where = array('to_id'=>$v['to_user_id'],'type'=>'to_topic');
                $sql = D('praises')->where($where)->select();

                foreach ($sql as $key=>$value){
                    $user[$key] = D('ucenter_member')->where('id='.$value['user_id'])->field('username,avatar')->find();
                    $result[$k]['praises_count'] = count($key);
                    $result[$k]['praises'] = $user[$key];
                }



            }

        }

        $this->ReturnClient(2,'文章评论！',$result);

    }


    /*
     * v达人
     *
     */

    public function vmaster(){
        $data['user_id'] = $_REQUEST['uid']==''?'0':$_REQUEST['uid'];
        $data['type'] = 'to_user';
        $pageNo =$_REQUEST['pageNo']==''?'1':$_REQUEST['pageNo'];
        $pageSize =$_REQUEST['pageSize']==''?'10':$_REQUEST['pageSize'];
        $follow = D('follow')->where($data)->field('to_id')->select();
        foreach ($follow as $k=>$v){
            $vfollow[$k]= $v['to_id'];
        }
        $sql = "SELECT to_id,count(to_id) as num from follow group by to_id order by num DESC limit ".$pageNo.",".$pageSize;
        $Model = M();
        $list = $Model->query($sql);

        foreach ($list as $k=>$v){
            $list[$k] = D('ucenter_member')->where('id='.$v['to_id'])->field('id,username,avatar')->find();
            if ($list[$k] == null){
                unset($list[$k]);
            }else{
                if ( in_array($list[$k]['id'],$vfollow)){
                    $list[$k]['status'] = '1';
                }else{
                    $list[$k]['status'] = '0';
                }

            if (strpos($list[$k]['avatar'],'http') !== false ){
                $list[$k]['avatar'] = $list[$k]['avatar'];
            }else{
                $list[$k]['avatar'] = 'http://'.$_SERVER['HTTP_HOST'].$list[$k]['avatar'];
            }
            }


        }
        $result = array_merge($list);
        $this->ReturnClient(2,'获取成功！',$result);

    }

    public function topic(){
        $data['user_id'] = $_REQUEST['uid']==''?'0':$_REQUEST['uid'];
        $data['type'] = 'to_topic';
        $pageNo =$_REQUEST['pageNo']==''?'1':$_REQUEST['pageNo'];
        $pageSize =$_REQUEST['pageSize']==''?'10':$_REQUEST['pageSize'];
        $follow = D('follow')->where($data)->field('to_id')->select();
        foreach ($follow as $k=>$v){
            $vfollow[$k]= $v['to_id'];
        }
        $list = D('community')->order('logos desc,id desc')->field('id,created_at,user_id,title,logos,pinglun,follow,praises')->limit($pageNo,$pageSize)->select();

        foreach ($list as $k=>$v){
            $user = D('ucenter_member')->where('id='.$v['user_id'])->find();
            $list[$k]['cover'] = unserialize($v['logos']);
            $list[$k]['username'] = $user['username'];
            $list[$k]['avatar'] = $user['avatar'];
            if ( in_array($list[$k]['id'],$vfollow)){
                $list[$k]['status'] = '1';
            }else{
                $list[$k]['status'] = '0';
            }


        }


//        $Model = M();
//        $where = array('f.user_id'=>$_REQUEST['uid']);
//
//        $list = $Model->table('community c')
//            ->join('follow f  ')
//            //->join('ucenter_member u on  u.id = c.user_id')
//            ->field('c.title,c.id,f.to_id as fid')
//            ->where($where)
//            ->order('c.id desc')
//            ->limit($pageNo,$pageSize)
//            ->select();
//var_dump($list);
        if(!$list){
            $this->ReturnClient(1,'获取失败！');
        }
        $this->ReturnClient(2,'获取成功！',$list);

    }

    public function my_topic(){
        $uid = $_REQUEST['uid']==''?'0':$_REQUEST['uid'];
        $pageNo =$_REQUEST['pageNo']==''?'1':$_REQUEST['pageNo'];
        $pageSize =$_REQUEST['pageSize']==''?'10':$_REQUEST['pageSize'];
        $list = D('community')->where('user_id='.$uid)->order('logos desc,id desc')->field('id,created_at,user_id,title,logos,pinglun,follow,praises')->limit($pageNo,$pageSize)->select();
        if(!$list){
            $this->ReturnClient(1,'获取失败！');
        }
        $this->ReturnClient(2,'获取成功！',$list);


    }

    /**
     * 收藏
     * @author houqiang
     */
    public function collection(){
        $data['user_id']  = $_REQUEST['uid']==''?'0':$_REQUEST['uid'];
        $data['to_id']  = $_REQUEST['to_id'];
        $data['type']  = $_REQUEST['type'];
        $sql = D('collection')->where($data)->find();
        if ($sql){
            D('collection')->where('id='.$sql['id'])->delete();
            $this->ReturnClient(1,'取消收藏成功！');
        }
        $data['created_at'] = time();
        $result = D('collection')->add($data);
        if ($result){
            $this->ReturnClient(2,'收藏成功！',$result);
        }else{
            $this->ReturnClient(1,'收藏失败！');
        }

    }


    /*
    * 积分记录
    */

    /**
     * 点赞
     * @author houqiang
     */
    public function praises(){
        $data['user_id']  = $_REQUEST['uid']==''?'0':$_REQUEST['uid'];
        $data['to_id']  = $_REQUEST['to_id'];
        $data['type']  = $_REQUEST['type'];
        $data['praises'] = $_REQUEST['praises']==''?'1':$_REQUEST['praises'];
        $data['created_at'] = time();
        $result = D('praises')->add($data);

        if ($data['type'] == 'to_article'){
            D('document')->where('id='.$data['to_id'])->setInc('praises',$data['praises']);
        }
        if ($data['type'] == 'to_comment'){
            D('comments')->where('id='.$data['to_id'])->setInc('favorites',$data['praises']);
        }
        if ($data['type'] == 'to_topic'){
            D('community')->where('id='.$data['to_id'])->setInc('praises',$data['praises']);
        }

        if ($result){
            $this->ReturnClient(2,'点赞成功！',$result);
        }else{
            $this->ReturnClient(1,'点赞失败！');
        }
    }

    /*
     * 积分历史
     */

    public function action_log(){
        $uid = intval($_REQUEST['uid']);
        $type = $_REQUEST['type'];
        $cid = $_REQUEST['cid'];
        if ($uid == '' || $type == ''){
            $this->ReturnClient(1,'失败，信息为空！');
        }

        if ($type == 'sign'){//获取用户数据
            //$sql = _m('users')->find(array('where'=>array('id'=>$uid),'field'=>'lately_sign'));
            $sql = D('ucenter_member')->where('id='.$uid)->field('lately_sign')->find();
            //echo "<pre>";
            //var_dump($sql);die;
            if($sql['lately_sign'] ==date("Y-m-d",time())){
                $this->ReturnClient(1,'今日已经签到，不能重复签到！');
            }
        }

        if ($cid){
            app_action_log($type, 'ucenter_member', $cid, $uid); //user_login 登录；sign 签到；serial_sign 连续签到;browse_the_news 浏览新闻
        }else{
            app_action_log($type, 'ucenter_member', $uid, $uid); //user_login 登录；sign 签到；serial_sign 连续签到;browse_the_news 浏览新闻
        }

        if ($type == 'sign'){
            app_action_log('serial_sign', 'users', $uid, $uid);//连续签到
            $sql="update ucenter_member set lately_sign =CURRENT_DATE() WHERE id =".$uid;
            $Model = M();
            $Model->execute($sql);
            $sql1="select sign_days from ucenter_member  WHERE id =".$uid;
            $user = $Model->query($sql1);
            //var_dump($user);
            $result['sign_days']=$user[0]['sign_days'];
            $this->ReturnClient(2,'签到成功！',$result);
        }
        $this->ReturnClient(2,'成功！');

    }

    /*
     * 消息
     */

    public function get_score()
    {

        $uid = $_REQUEST['uid']==''?'0':$_REQUEST['uid'];
        $pageNo =$_REQUEST['pageNo']==''?'1':$_REQUEST['pageNo'];
        $pageSize =$_REQUEST['pageSize']==''?'10':$_REQUEST['pageSize'];
        $list = D('action_log')->where('user_id='.$uid)->order('id desc')->limit($pageNo,$pageSize)->select();
        if(!$list){
            $this->ReturnClient(1,'无历史记录！');
        }
        $this->ReturnClient(2,'积分历史！',$list);
    }

    public function record_list(){
        $type = $_REQUEST['type'];
        if (!$type){
            $this->ReturnClient(1,'参数错误!');
        }
        $uid = $_REQUEST['uid']==''?'0':$_REQUEST['uid'];
        $pageNo =$_REQUEST['pageNo']==''?'0':$_REQUEST['pageNo'];
        $pageSize =$_REQUEST['pageSize']==''?'10':$_REQUEST['pageSize'];
        if ($uid) {
            if ($type == 'dynamic') {
                $res['user_id'] = $uid;
                $list = D('messages')->where($res)->order('id desc')->limit($pageNo, $pageSize)->select();

                foreach ($list as $k => $v) {
                    if ($v['model'] == 'ucenter_member') {
                        //echo "<pre>";
                        //var_dump($v['to_id']);die;
                        $res = D($v['model'])->where('id='."'".$v['to_id']."'")->find();
                        $list[$k]['member']['id'] = $res['id'];
                        $list[$k]['member']['name'] = $res['username'];
                        $list[$k]['member']['logo'] = $res['avatar'];
                        $list[$k]['article']['id'] = '';
                        $list[$k]['article']['name'] = '';
                        $list[$k]['article']['logo'] = '';
                    }else if ($v['model'] == 'document'){
                        //echo "<pre>";
                       // var_dump($v['to_id']);die;
                        $res = D($v['model'])->where('id=' . $v['data_id'])->find();
                        $list[$k]['article']['id'] = $res['id'];
                        $list[$k]['article']['title'] = $res['title'];
                        $list[$k]['article']['logo'] = $res['cover'];
                        $user = D('ucenter_member')->where('id='.$res['uid'])->find();
                        $list[$k]['member']['id'] = $user['id'];
                        $list[$k]['member']['name'] = $user['username'];
                        if (strpos($user['avatar'],'http') !== false ){
                            $list[$k]['member']['logo'] = $user['avatar'];
                        }else{
                            $list[$k]['member']['logo'] = 'http://'.$_SERVER['HTTP_HOST'].$user['avatar'];
                        }

                    }else{
                        unset($list[$k]);
                    }
                }
            }

            if ($type == 'message') {
                $res['to_id'] = $uid;
                $list = D('messages')->where($res)->order('id desc')->limit($pageNo, $pageSize)->select();
                foreach ($list as $k => $v) {
                    if ($v['model'] == 'ucenter_member') {
                        $res = D($v['model'])->where('id=' . $v['user_id'])->find();
                        $list[$k]['member']['id'] = $res['id'];
                        $list[$k]['member']['name'] = $res['username'];
                        $list[$k]['member']['logo'] = $res['avatar'];
                        $list[$k]['article']['id'] = '';
                        $list[$k]['article']['title'] = '';
                        $list[$k]['article']['logo'] = '';
                    }else if ($v['model'] == 'document'){
                        $res = D($v['model'])->where('id=' . $v['data_id'])->find();
                        $list[$k]['article']['id'] = $res['id'];
                        $list[$k]['article']['title'] = $res['title'];
                        $list[$k]['article']['logo'] = $res['cover'];
                        $user = D('ucenter_member')->where('id='.$v['user_id'])->find();
                        $list[$k]['member']['id'] = $user['id'];
                        $list[$k]['member']['name'] = $user['username'];
                        if (strpos($user['avatar'],'http') !== false ){
                            $list[$k]['member']['logo'] = $user['avatar'];
                        }else{
                            $list[$k]['member']['logo'] = 'http://'.$_SERVER['HTTP_HOST'].$user['avatar'];
                        }

                    }else{
                        unset($list[$k]);
                    }
                }
            }

//echo "<pre>";var_dump($res);

//            $list = D('messages')->where($res)->order('id desc')->limit($pageNo,$pageSize)->select();
//            foreach ($list as $k=>$v){
//                if($v['model'] == 'ucenter_member'){
//                    //$list[$k]['data'] = D($v['model'])->where('id='.$v['to_id'])->find();
//                    $res =  D($v['model'])->where('id='.$v['to_id'])->find();
//                    $list[$k]['data']['id'] = $res['id'];
//                    $list[$k]['data']['name'] = $res['username'];
//                    $list[$k]['data']['logo'] = $res['avatar'];
//                    $list[$k]['data']['user_id'] = '';
//
//
//                }else{
//                    unset($list[$k]);
//                }

//                if($v['model'] == 'document'){
//                    $res =  D($v['model'])->where('id='.$v['to_id'])->find();
//                    $list[$k]['data']['id'] = $res['id'];
//                    $list[$k]['data']['name'] = $res['title'];
//                    $list[$k]['data']['logo'] = $res['cover'];
//                    $list[$k]['data']['user_id'] = $;
//
//                }
//                if($v['model'] == 'community'){
//                    $res =  D($v['model'])->where('id='.$v['to_id'])->find();
//                    $list[$k]['data']['id'] = $res['id'];
//                    $list[$k]['data']['name'] = $res['title'];
//                    $logo = unserialize($res['logos']);
//                    $list[$k]['data']['user_id'] = $res['user_id'];
//                }
//            }

            if (!$list) {
                $this->ReturnClient(1, '无记录信息！');
            }
            $this->ReturnClient(2, '记录信息！', $list);
        }
    }

    //杂志列表
    function magazine(){
        $code = $_REQUEST['code'];
        $pageNo =$_REQUEST['pageNo']==''?'0':$_REQUEST['pageNo'];
        $pageSize =$_REQUEST['pageSize']==''?'10':$_REQUEST['pageSize'];
        $list = D('magazine')->where('code='.'"'.$code.'"')->field('id,title,logo')->order('id desc')->limit($pageNo,$pageSize)->select();
        if(!$list){
            $this->ReturnClient(1,'无杂志！');
        }
        $this->ReturnClient(2,'杂志列表！',$list);

    }

    function search(){
        $title = $_REQUEST['title'];
        $pageNo =$_REQUEST['pageNo']==''?'0':$_REQUEST['pageNo'];
        $pageSize =$_REQUEST['pageSize']==''?'10':$_REQUEST['pageSize'];
        $map['title']    =   array('like', '%'.$title.'%');

        $result = D('document')->where($map)->limit($pageNo,$pageSize)->field('id,title,category_id,cover')->order('id desc')->select();
        $result1 = D('community')->where($map)->limit($pageNo,$pageSize)->field('id,title,logos')->order('id desc, logos desc')->select();
        foreach ($result1 as $k =>$v){
            $logo = unserialize($v['logos']);
            $result1[$k]['cover'] = $logo[0];
            $result1[$k]['type'] = 'topic';
        }
        $data = array_merge($result,$result1);
        $this->ReturnClient(2,'杂志列表！',$data);
    }







}
